﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ModEngine;
using System.Threading;
using Iris.Engine;
using System.IO;
using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Shell.PropertySystem;

namespace Iris.ImageProcess
{
    class ImageSetter : IModule
    {
        private Thread threadUpdate;

        public ImageSetter() {
            SortCore.singleton.BeginUpdateFiveStarEvent += OnCommitDataEvent;
        }

        string IModule.getDescriptionModule()
        {
            return "Sauvegarde les notes dans les meta-données via exifTool";
        }

        string IModule.getNameModule()
        {
            return "ImageSetter";
        }

        string IModule.getVersion()
        {
            return "1.0.0";
        }

        void IModule.Dispose()
        {

            if (threadUpdate != null)
            {
                threadUpdate.Join();
            } 
        }

        public void OnCommitDataEvent(List<ImageSort> images)
        {
            if (threadUpdate != null)
            {
                threadUpdate.Join();
            }

            threadUpdate = new Thread(runUpdate);
            threadUpdate.Priority = ThreadPriority.Highest;
            threadUpdate.Start(images);
        }

        private void runUpdate(object ListeSort)
        {

            List<ImageSort> images  = (List<ImageSort>)ListeSort;
            if (images.Count > 0)
            {
                for(int i=0; i< images.Count; i++)
                {
                    try
                    {
                        UpdateFiveStar(
                            images[i].DataSource,
                            images[i].Rating
                        );

                    }
                    catch (Exception e)
                    {
                        SortCore.singleton.UpdateFiveStarFailed(string.Format("{0}: {1}", images[i].DataSource.Name, e.Message));
                    }
                    finally {
                        SortCore.singleton.UpdateFiveStar(images[i].DataSource.Name, i, images.Count-1);
                    }
                }
            }
        }


        private static void UpdateFiveStar(FileInfo file, uint percent) {

            // Get a ShellObject (wrapping native IShellItem) for the image you want to propertize
            ShellObject picture = ShellObject.FromParsingName(file.FullName);
            
            //var rating = picture.Properties.GetProperty(SystemProperties.System.Rating);

            // To set a property, get a PropertyWriter for the ShellObject like this.
            ShellPropertyWriter writer = picture.Properties.GetPropertyWriter();

            // Set properties as you wish. Keep in mind that the value field must be the same type as required by the property. (Rating requires int, subject requires string).
            writer.WriteProperty(SystemProperties.System.Rating, (uint?)percent);

            // Closing the writer actually commits the changes.
            writer.Close();
        }
    }
}
